package com.vstartek.dm.mongo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.vstartek.dm.mongo.dao.VideoTagDao;
import com.vstartek.dm.mongo.model.VideoTag;
import com.vstartek.dm.mongo.service.VideoTagService;
import com.vstartek.dm.util.Page;
import com.vstartek.dm.util.PageParam;
import com.vstartek.dm.util.SQLUtil;
import com.vstartek.dm.util.TimeUtil;
@Service
public class VideoTagServiceImpl implements VideoTagService {

	@Autowired
	VideoTagDao videoTagDao;
	
	@Override
	public Page<VideoTag> findByPage(PageParam pageParam, VideoTag videoTag) {
		
		Query query = new Query();
		if(videoTag.getName()!=null)
		query.addCriteria(new Criteria().orOperator(Criteria.where("other_name").regex(".*?" + videoTag.getName() + ".*"),Criteria.where("name").regex(".*?" + videoTag.getName() + ".*")));
		
		SQLUtil.dealTimeWhere("lastUpdateTime", query, pageParam);
		
		Long num = videoTagDao.totalnum(query);
		List<VideoTag> vlist = videoTagDao.findByList(query,pageParam);
		Page<VideoTag> page = new Page<VideoTag>(vlist,num,pageParam);
		return page;
		
	}

	@Override
	public VideoTag saveVideoTag(VideoTag videoTag) {
		videoTag.setLastUpdateTime(TimeUtil.getNowSTime());
		if(videoTagDao.save(videoTag)){
			return videoTag;
		}
		return null;
	}

	@Override
	public boolean updateVideoTag(VideoTag videoTag) {
		videoTag.setLastUpdateTime(TimeUtil.getNowSTime());
		return videoTagDao.update(videoTag);
		
	}

	@Override
	public VideoTag findById(Object id) {
		// TODO Auto-generated method stub
		return videoTagDao.findById(id);
	}
	
	

}
